﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WebDownloader.Classes;

namespace WebDownloader
{
    public partial class Form1 : Form
    {
        enum DownloadResType
        {
            /// <summary>
            /// 类似于BBS里的多个Link
            /// </summary>
            BBSList,

            /// <summary>
            /// 连续地址的图片
            /// </summary>
            Image,

            Count,
        }

        BBSListSetting m_currSettingForm;
        string m_bbsListParam="?";
        int m_bbsListParamStep = 1;

        public Form1()
        {
            InitializeComponent();
        }

        private void btn_startDownload_Click(object sender, EventArgs e)
        {
            //Check folder & link
            string linkAddress = tb_addressToDownload.Text;
            string saveToFolder = tb_saveTo.Text;

            if (string.IsNullOrEmpty(saveToFolder) 
                || string.IsNullOrEmpty(linkAddress))
            {
                return;
            }

            //Check folder:
            if (!Directory.Exists(saveToFolder))
            {
                Directory.CreateDirectory(saveToFolder);
                if (!Directory.Exists(saveToFolder))
                {
                    //--Illegal folder:
                    MessageBox.Show("Ah~~~~Illegal directory.");
                    return;
                }
            }

            //Do it:
            Downloader.StartDownloader(linkAddress,saveToFolder);
        }

        private void btn_broswer_Click(object sender, EventArgs e)
        {
            //Open a folder broswer dialog
            FolderBrowserDialog browserDialog = new FolderBrowserDialog();
            DialogResult result = browserDialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                tb_saveTo.Text = browserDialog.SelectedPath;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            #region 控件的设置：
            //允许线程更改UI控件：
            CheckForIllegalCrossThreadCalls = false;

            //对Debug.Log设置对应的回调：在输出控制台的同时输出到UI控件上
            MyDebug.Debug.SetCallbackLogStr((strMsg) =>
                {
                    lb_logView.Items.Add(strMsg);
                });

            //增加可选的下载类型，默认index
            for (int i = 0; i < (int)DownloadResType.Count; i++)
            {
                cbDownloadType.Items.Add((DownloadResType)i);
            }
            cbDownloadType.SelectedIndex = 0;
            #endregion
        }

        /// <summary>
        /// 下载类型的选择项更改时，触发对应设置按钮的显示与否
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbDownloadType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbDownloadType.SelectedIndex == (int)DownloadResType.BBSList)
            {
                btn_BBSListSetting.Visible = true;
            }
            else
            {
                btn_BBSListSetting.Visible = false;
            }
        }

        /// <summary>
        /// 开启对BBSList的设定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_BBSListSetting_Click(object sender, EventArgs e)
        {
            //--已经显示了这个窗口了，删除之
            if (m_currSettingForm != null)
            {
                m_currSettingForm.Close();
                m_currSettingForm.Dispose();
            }
            
            m_currSettingForm = new BBSListSetting();
            
            m_currSettingForm
                //--初始化窗口值：
                .InitData(m_bbsListParam,m_bbsListParamStep)
                //--确认时的回调：
                .AddConfrimListener((param,paramStep) =>
                {
                    MessageBox.Show("Comfirm & closed>"+param+" step:"+paramStep);
                    m_bbsListParam = param;
                    m_bbsListParamStep = paramStep;
                })
                //--显示这个窗口：
                .Show();
        }
    }
}
